/*
 * 文件名：adInfoController.java	 
 * 时     间：下午4:20:10
 * 作     者：liyf       
 * 版     权： 2014-2022  智慧园区, 公司保留所有权利.
 * 联     系：www.szyungu.com
 */
package com.cz073586.technology.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.cz073586.common.contants.CommonContans;
import com.cz073586.common.controller.BaseController;
import com.cz073586.technology.model.AdInfo;
import com.cz073586.technology.service.face.AdInfoService;
import com.cz073586.tools.privilege.ResourceAndOperate;
import com.cz073586.tools.util.UUIDGenerateUtil;

/**
 * @ClassName: adInfoController
 * @Description: 广告信息
 * @author zengqj
 * @date 2015年1月20日 下午4:20:10
 */
@RestController
@RequestMapping(value = "/adManager", method = RequestMethod.POST)
@ResourceAndOperate(
		moduleID = "com.technology.ad", 
		moduleName = "广告管理", 
		parentModuleID = "com.technology", 
		parentModuleName = "昱显科技项目", 
		resouceDesc = "管理广告"
		)
public class AdInfoController extends BaseController
{
	@Autowired
	private AdInfoService adInfoService;
	
	/**
	 * 
	 * @方法名称: queryServicesByPage
	 * @描述: 查询广告列表
	 *
	 * @参数:
	 * @param page
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:liyf
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryAdByPage.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.ad.queryadListByPage", 
			moduleName = "通过分页对象查询广告", 
			parentModuleID = "com.technology.ad", 
			parentModuleName = "广告管理", 
			resouceDesc = "管理广告"
			)
	public ModelMap queryAdListByPage(@RequestBody AdInfo adInfo)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			resultMap.put(CommonContans.STRUTS_DATA, adInfoService.queryByObject(adInfo));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/queryAdList.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.ad.queryAdList", 
			moduleName = "查询滑屏和词条广告", 
			parentModuleID = "com.technology.ad", 
			parentModuleName = "广告管理", 
			resouceDesc = "查询滑屏和词条广告,无需入参，返回所有的滑屏和词条广告。"
			)
	public ModelMap queryAdList(@RequestBody AdInfo adInfo)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			
			resultMap.put(CommonContans.STRUTS_DATA, adInfoService.queryAdList());
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @方法名称: queryadById
	 * @描述: 通过广告ID查询
	 *
	 * @参数:
	 * @param adId
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:liyf
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryadById.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.ad.queryadById", 
			moduleName = "通过ID查询单个对象.", 
			parentModuleID = "com.technology.ad", 
			parentModuleName = "广告管理", 
			resouceDesc = "通过ID查询单个对象."
			)
	public ModelMap queryadById(String adId)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			
			resultMap.put(CommonContans.STRUTS_DATA,
					adInfoService.selectByPrimaryKey(adId));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：添加广告信息，uuid由代码写入
	 * 
	 * @Title: insertad
	 * @param @param ad
	 * @param @return
	 * @return ModelMap
	 * @ad Administrator 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/insertAd.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.ad.insertAd", 
			moduleName = "添加广告", 
					resouceDesc = "添加广告",
			parentModuleID = "com.technology.ad", 
			parentModuleName = "广告管理"
			)
	public ModelMap insertAd(@RequestBody AdInfo ad)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			ad.setUuid(UUIDGenerateUtil.generate());
			resultMap.put(CommonContans.STRUTS_DATA, adInfoService.insert(ad));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：根据ad主机修改广告信息
	 * 
	 * @Title: updatead
	 * @param @param ad
	 * @param @return
	 * @return ModelMap
	 * @ad Administrator 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/updatead.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.ad.updateAd", 
			moduleName = "更新", 
		    resouceDesc = "更新广告",
			parentModuleID = "com.technology.ad", 
			parentModuleName = "广告管理"
			)
	public ModelMap updateAd(@RequestBody AdInfo ad)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			resultMap.put(CommonContans.STRUTS_DATA,
					adInfoService.updateByPrimaryKeySelective(ad));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：根据主键删除广告
	 * 
	 * @Title: deletead
	 * @param @param adId
	 * @param @return
	 * @return ModelMap
	 * @ad Administrator 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/deleteAd.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.ad.deleteAd", 
			moduleName = "删除广告", 
		    resouceDesc = "删除广告",
			parentModuleID = "com.technology.ad", 
			parentModuleName = "广告管理"
			)
	public ModelMap deleteAd(String adId)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			resultMap.put(CommonContans.STRUTS_DATA,
					adInfoService.deleteByPrimaryKey(adId));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/addAdInfo.action",method=RequestMethod.POST,produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.ad.addAdInfo", 
			moduleName = "添加广告", 
			resouceDesc = "发布广告，带附件的",
			parentModuleID = "com.technology.ad", 
			parentModuleName = "广告管理"
			)
    public ModelAndView addAdInfo(AdInfo adInfo,@RequestParam MultipartFile[] myfiles, HttpServletRequest request) throws IOException{  
        
        
        ModelMap resultMap = new ModelMap();
        adInfo.setUuid(UUIDGenerateUtil.generate());
        adInfo.setCreateAt(new Date());
		try
		{
			//如果只是上传一个文件，则只需要MultipartFile类型接收文件即可，而且无需显式指定@RequestParam注解  
	        //如果想上传多个文件，那么这里就要用MultipartFile[]类型来接收文件，并且还要指定@RequestParam注解  
	        //并且上传多个文件时，前台表单中的所有<input type="file"/>的name都应该是myfiles，否则参数里的myfiles无法获取到所有上传的文件  
			String file="";
	        for(MultipartFile myfile : myfiles){  
	            if(myfile.isEmpty()){  
	                System.out.println("文件未上传");  
	            }else{  
	                System.out.println("文件长度: " + myfile.getSize());  
	                System.out.println("文件类型: " + myfile.getContentType());  
	                System.out.println("文件名称: " + myfile.getName());  
	                System.out.println("文件原名: " + myfile.getOriginalFilename());  
	                System.out.println("========================================");  
	                //如果用的是Tomcat服务器，则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中  
	                String realPath = request.getSession().getServletContext().getRealPath("/image/adInfo");
	                String fileName = System.currentTimeMillis() + myfile.getOriginalFilename();
	                file += "/image/adInfo/"+fileName+",";
	                //这里不必处理IO流关闭的问题，因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉，我是看它的源码才知道的 
	                FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, fileName));  
	            }  
	        }  
	        adInfo.setImgUrl(file.substring(0, file.length()-1));
	        adInfoService.insert(adInfo);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
//        return resultMap; 
		return new ModelAndView(
				"redirect:/admin/view/technology/adinfo/adinfolist.jsp");
    }
}
